<?php

/**
 * patrocinador actions.
 *
 * @package    showrrascoeviola
 * @subpackage patrocinador
 * @author     Foxsis
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class patrocinadorActions extends sfActionsFoxsis
{
  public function executeIndex(sfWebRequest $request)
  {
    return $this->forward('patrocinador', 'list');
  }

    public function executeList()
	{
        $paginaAtual = $this->_setActualPage();
        $this->filter = $this->_getFilter();
        $listQuery = PatrocinadorTable::consultarPatrocinador($this->filter);

        ## cria o paginador
        $this->getContext()->getConfiguration()->loadHelpers('Url');
		$this->pager_layout = new Foxsis_Pager_Layout(
			$pager = new Doctrine_Pager(
				$listQuery,
				$paginaAtual,
				NUM_LIST_ITENS
			),
			new Doctrine_Pager_Range_Sliding(array('chunk' => MAX_PAGER_LINKS)),
			url_for('patrocinador/list')
		);

		## executa a consulta
		$this->patrocinador_list = $this->pager_layout->execute(array(), Doctrine::HYDRATE_ARRAY);

        $this->setTemplate('index');
    }

    public function executeFilter()
	{
	    $filter = new Filtro(
			$this->getRequestParameter(sfActionsFoxsis::FILTER_SESSION_USER_ATTRIBUTE)
		);

		$filter->setCulture($this->getUser()->getCulture());
		$this->getUser()->setAttribute('pg_num',1,$this->getModuleName());

		$this->getUser()->setAttribute(
			sfActionsFoxsis::FILTER_SESSION_USER_ATTRIBUTE,
			serialize($filter),
			$this->getModuleName()
		);

		return $this->redirect($this->getModuleName().'/list');
	}

  public function executeShow(sfWebRequest $request)
  {
    $this->patrocinador = Doctrine::getTable('Patrocinador')->find(array($request->getParameter('id_patrocinador')));
    $this->forward404Unless($this->patrocinador);
  }

  public function executeNew(sfWebRequest $request)
  {
    $this->form = new PatrocinadorForm();
  }

  public function executeCreate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST));

    $this->form = new PatrocinadorForm();

    $this->processForm($request, $this->form);

    $this->forward($this->getModuleName(), 'new');
  }

  public function validateCreate()
  {
  	return true;
  }

  public function handleErrorCreate()
  {
    return $this->forward($this->getModuleName(), 'new');
  }

  public function executeEdit(sfWebRequest $request)
  {
    $this->forward404Unless($patrocinador = Doctrine::getTable('Patrocinador')->find(array($request->getParameter('id_patrocinador'))), sprintf('Object patrocinador does not exist (%s).', $request->getParameter('id_patrocinador')));
    $this->form = new PatrocinadorForm($patrocinador);
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
    $this->forward404Unless($patrocinador = Doctrine::getTable('Patrocinador')->find(array($request->getParameter('id_patrocinador'))), sprintf('Object patrocinador does not exist (%s).', $request->getParameter('id_patrocinador')));
    $this->form = new PatrocinadorForm($patrocinador);

    $this->processForm($request, $this->form);

    $this->forward($this->getModuleName(), 'edit');
  }

  public function validateUpdate()
  {
  	return true;
  }

  public function handleErrorUpdate()
  {
    return $this->forward($this->getModuleName(), 'edit');
  }

  public function executeDelete(sfWebRequest $request)
  {
    try
    {
    $request->checkCSRFProtection();

    $this->forward404Unless($patrocinador = Doctrine::getTable('Patrocinador')->find(array($request->getParameter('id_patrocinador'))), sprintf('Object patrocinador does not exist (%s).', $request->getParameter('id_patrocinador')));
    $patrocinador->delete();

    $this->redirect('patrocinador/index');
    }
    catch(Excepiton $e)
	{
		$this->getUser()->setFlash('erro', $e->getMessage());
    	$this->redirect('patrocinador/index');
	}

  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
  	try
  	{
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid())
    {
      $patrocinador = $form->save();
	  $this->getUser()->setFlash('mensagem', 'Dados salvos com sucesso.');
      $this->redirect('patrocinador/edit?id_patrocinador='.$patrocinador->getIdPatrocinador());
    }
    else
    {
    throw new Exception($form->getErrorSchema());
    }
    }
    catch(Exception $e)
	{
		$this->getUser()->setFlash('erro', $e->getMessage());
	}
  }
}
